package com.food.chain.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.food.chain.common.util.EncryptUtil;
import com.food.chain.constant.Constant;
import com.food.chain.entity.User;
import com.food.chain.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private HttpServletResponse response;
	
	@Autowired
	private HttpServletRequest request;
	
	/**
	 * 用户认证登录
	 * @param jsonString
	 * @return
	 */
	@RequestMapping(value = "/login")
	@ResponseBody
	public JSONObject userLogin(String userId, String password) {
		JSONObject returnMap = new JSONObject();
		
			Subject currentUser = SecurityUtils.getSubject();
			// 封装登录信息
			UsernamePasswordToken token = 
					new UsernamePasswordToken(userId, password);
			// 登录
			currentUser.login(token);
			
			Session session = currentUser.getSession();
			
			System.out.println("session-id:"+session.getId());
			System.out.println("session-host:"+session.getHost());
			// 默认会话有效时间是30分钟
			System.out.println("session-过期时间:"+session.getTimeout());
			// 更新IP地址
			userService.updateUserLoginIP(userId);
			returnMap.put("success", true);
			returnMap.put("redirect", "/index.jsp");
			//returnMap = JSONObject.parseObject(Constant.jsonStr_exception);
			//response.setStatus(500);
		
		return returnMap;
	}
	
	/**
	 * 退出系统
	 */
	@RequestMapping(value = "/logout")
	@ResponseBody
	public void logout() {
		// 退出系统
		SecurityUtils.getSubject().logout();
	}
	
	/**
	 * 用户列表
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/users")
	@ResponseBody
	public Map<String, Object> getUsers(String jsonString) {
		Map<String, Object> result = new HashMap<>();
		try {
			result = userService.GetUserList(jsonString);
		} catch (Exception e) {
			result = JSONObject.parseObject(Constant.jsonStr_exception);
		}
		
		return result;
	}
	
	@RequestMapping(value = "/add.do", method = RequestMethod.POST)
	public String addUser(User user) throws Exception{
		
		int index = userService.addUser(user);
		System.out.println(index);
		return "redirect:users";
	}
	
	@RequestMapping(value = "/newUser", method = RequestMethod.GET)
	public String userAdd(){
		return "addUser";
	}
	
	
	/**
	 *  列表导出
	 * @throws Exception 
	 */
	@RequestMapping(value = "/exportUserInfo")
	@ResponseBody
	public void exportUserInfo(String jsonString, HttpServletResponse response) throws Exception {
		userService.exportUserInfo(jsonString,response);
	}
	
	/**
	 *  前端导出所需数据
	 * @throws Exception 
	 */
	@RequestMapping(value = "/exportUserInfoList")
	@ResponseBody
	public Map<String, Object> exportUserInfoList(String jsonString) throws Exception {
		Map<String, Object> result = new HashMap<>();
		try {
			result = userService.exportUserInfoList(jsonString);
		} catch (Exception e) {
			result = JSONObject.parseObject(Constant.jsonStr_exception);
		}
		
		return result;
	}
	
	
	/**
	 * 更新用户密码
	 * @param oldpwd：旧密码
	 * @param newpwd：新密码
	 * @return
	 */
	@RequestMapping(value = "/updatepwd")
	@ResponseBody
	public Map<String, Object> updateUserPwd(String oldpwd, String newpwd) {
		Map<String, Object> result = new HashMap<>();
		try {
			result = userService.changePwd(oldpwd, newpwd);
		} catch (Exception e) {
			result = JSONObject.parseObject(Constant.jsonStr_exception);
			response.setStatus(500);
		}
		
		return result;
	}
	
	
}
